package com.hassean.we.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hassean.we.admin.common.constant.CommonConstant;
import com.hassean.we.admin.common.util.PrepareCall;
import com.hassean.we.admin.common.util.template.MD5Util;
import com.hassean.we.admin.model.entity.TbMessage;
import com.hassean.we.admin.model.entity.TbSimFlow;
import com.hassean.we.admin.model.entity.TbSimInfo;
import com.hassean.we.admin.service.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by yajun on 2019/4/9.
 */
@Service
public class HttpClientServiceImpl implements HttpClientService {
    @Autowired private TbSimInfoService tbSimInfoService;
    @Autowired private TbCallService tbCallService;
    @Autowired private TbMessageService tbMessageService;
    @Autowired private TbSimFlowService tbSimFlowService;
    @Override
    /**
     * 自己测试HttpCilent
     */
    public Object getData()throws JSONException {
        Object object = null;
        String url = "http://47.105.159.199:1025/tbCall/1";
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpGet httppost = new HttpGet(url);
        // 创建参数队列


        UrlEncodedFormEntity uefEntity;
        try {


            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity  = response.getEntity();
                if (entity != null) {
                    System.out.println("--------------------------------------");
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
                    System.out.println("--------------------------------------");

                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

     return  object;
    }

    /**
     * 调综合平台的权限认证，弃用
     * @param token
     * @return
     */
    @Override
    public boolean passAuth(String token) {

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("http://47.105.84.34:9999/auth/oauth/check_token");
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("token",token));
        //添加参数
        try {
            post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();
            String result =EntityUtils.toString(entity, "utf-8");
            System.out.println(result);
            if( result.contains("user_name")){
                return true;//认证通过
            } else {
                return false;//认证没有通过
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return  false;
    }

    /**
     * 获取轨迹位置
     * @return
     */

    @Override
    public Map<String ,Object>getLocation() {
        Map<String, Object> map = new HashMap<>();
        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url+"/tisp/getLocation");

        String spName = CommonConstant.spName;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String requestTime = sdf.format(new Date());

        //开始时间，从数据表中获取最大的时间
        Map<String,Object>timeMap=getMAXaddTime();
         String beginTime  =(String)timeMap.get("beginTime");
        //结束时间，在数据表中最大时间+10分钟
         String endTime = (String)timeMap.get("endTime");;

        String spPwd =CommonConstant.spPwd;
        String str = spName+requestTime+spPwd;
        MD5Util mD5Util = new MD5Util();
        String   token =   mD5Util.getMD5String(str);//MD5加密一次


        //把参数转换为json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("spName", spName);
        jsonParam.put("requestTime", requestTime);
        jsonParam.put("token", token);
        List<String>strList = new ArrayList<>();
        //strList.add("17406000024");
        //strList.add("17406001111");
        strList = null;
        jsonParam.put("beginTime",beginTime);
        jsonParam.put("endTime",endTime);
        jsonParam.put("strList",strList);//如果strList为空，取全部
        //jsonParam.put("strList",strList);//如果strList为空，取全部
        StringEntity json = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
        json.setContentEncoding("UTF-8");
        json.setContentType("application/json");

        // System.out.println(jsonParam.toString());

        //添加参数
        try {
            post.setEntity(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");
             map = JSON.parseObject(result, Map.class);
            System.out.println("map:"+map);
            //map:{result=0, list=[["17406000024","108.957944","34.13625","20181109135233"],["17406001111","120.853778","30.741556","20181102154616"]]}
            System.out.println(map.get("result"));
            System.out.println("result:"+result);


        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * SIM卡的流量开关
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean simFlowOffAndON(Long id,Long status,String phone) {
        Map<String, Object> map = new HashMap<>();
        Boolean falg = true;
        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url + "/tisp/simFlowOffAndOn");

        String spName = CommonConstant.spName;//还没有这个参数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String requestTime = sdf.format(new Date());
        String spPwd = CommonConstant.spPwd;//还没有这个参数
        String token = spName + requestTime + spPwd;
        //token是否需要MD5加密
        MD5Util mD5Util = new MD5Util();
        token = mD5Util.getMD5String(token);//MD5加密一次


        //把参数转换为json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("spName", spName);
        jsonParam.put("requestTime", requestTime);
        jsonParam.put("token", token);
        List<List>phoneNumber = new ArrayList<>();
        List<Object>list = new ArrayList<>();
        list.add("18822834812");
        list.add(phone);
        list.add(0);//没有定义
        phoneNumber.add(list);
        jsonParam.put("phoneNumber",phoneNumber);
        StringEntity json = new StringEntity(jsonParam.toString(), "utf-8");//解决中文乱码问题
        json.setContentEncoding("UTF-8");
        json.setContentType("application/json");

        System.out.println(jsonParam.toString());

        //添加参数
        try {
            post.setEntity(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");
            map = JSON.parseObject(result, Map.class);
            System.out.println("map:" + map);
            Integer  state = Integer.parseInt(map.get("result").toString());
            //map:{result=0, list=[["17406000024","108.957944","34.13625","20181109135233"],["17406001111","120.853778","30.741556","20181102154616"]]}
            System.out.println(map.get("result"));
            System.out.println("result:" + result);

            if(state==0){
                TbSimInfo tbSimInfo = tbSimInfoService.selectById(id);
                tbSimInfo.setStatus(status);//流量开关默认0开，1关
                falg =   tbSimInfoService.updateById(tbSimInfo);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        return falg;
    }






    /**
     * 短信记录
     * @return
     */
    @Override
    public  Map<String ,Object> tbMessageRrcord() {
        Map<String ,Object> map =null;
        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url+"/tisp/getSMSRecord");

        String spName = CommonConstant.spName;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String requestTime = sdf.format(new Date());

        //获取通话记录表最大时间
        List<Map<String, Object>> list =  tbMessageService.getMaxAddTime();
        Date addTime = null;
        String beginTime = null;
        String  endTime = null;
        if(!list.isEmpty()){
            addTime = (Date) list.get(0).get("add_time");
            //开始时间
            beginTime = sdf.format(addTime);
            Long endTimes =  addTime .getTime() + 60*1000*9 ;//9分钟
            Date date = new Date();
            date.setTime(endTimes);
            //结束时间
            endTime =  sdf.format(date);

        }else{
            //如果数据是空的时候，获取此刻前9分钟内的数据
            Long beginTimes =new Date().getTime()- 60*1000*9 ;//9分钟前
            Date date = new Date();
            date.setTime(beginTimes);
            //开始时间
            beginTime = sdf.format(date);
            //结束时间
            endTime =  sdf.format( new Date());
        }



        String spPwd =CommonConstant.spPwd;
        String str = spName+requestTime+spPwd;
        MD5Util mD5Util = new MD5Util();
        String   token =   mD5Util.getMD5String(str);//MD5加密一次


        //把参数转换为json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("spName", spName);
        jsonParam.put("requestTime", requestTime);
        jsonParam.put("token", token);
        List<String>strList = new ArrayList<>();
        strList.add("17406000024");
        strList=null;
        jsonParam.put("beginTime",beginTime);
        jsonParam.put("endTime",endTime);
        jsonParam.put("strList",strList);//如果strList为空，取全部
        StringEntity json = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
        json.setContentEncoding("UTF-8");
        json.setContentType("application/json");

        System.out.println(jsonParam.toString());

        //添加参数
        try {
            post.setEntity(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");
            map = JSON.parseObject(result, Map.class);
            System.out.println("map:"+map);
            //map:{result=0, list=[["17406000024","108.957944","34.13625","20181109135233"],["17406001111","120.853778","30.741556","20181102154616"]]}
            System.out.println(map.get("result"));
            System.out.println("result:"+result);

            //  List<List>list = (List)map.get("list");

//            list.get(0).get(0);//List里面套List
//            System.out.println("list.get(0).get(0);"+ list.get(0).get(0));
//            System.out.println("list.get(0).get(1);"+ list.get(0).get(1));
//            System.out.println("list.get(0).get(2);"+ list.get(0).get(2));
//            System.out.println("list.get(0).get(3);"+ list.get(0).get(3));//20181109135233  时间需要格式化
            //还没写完

            // System.out.println("list.size():"+list.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 点对点发短信
     * @param tbMessage
     * @return
     */
    @Override
    public  Map<String ,Object> pointTOpointSend(TbMessage tbMessage) {
        Map<String, Object> map = new HashMap<>();
        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url+"/tisp/getSendMessage");

        String spName = CommonConstant.spName;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String requestTime = sdf.format(new Date());

        String spPwd =CommonConstant.spPwd;
        String str = spName+requestTime+spPwd;
        MD5Util mD5Util = new MD5Util();
        String   token =   mD5Util.getMD5String(str);//MD5加密一次
        //通过被叫终端id获取电话号码

        String recNumber =null;//如果是系统平台，就没有发送人电话号码，只有接收人电话号码

        List<Map<String, Object>>list = tbSimInfoService.getPhoneByTerminalId(tbMessage.getPassivityTerminalId());
        if(!list.isEmpty()){
            recNumber= (String)list.get(0).get("sim_number");

        }

        String content = tbMessage.getContent();//"我是短信内容";
        //把参数转换为json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("spName", spName);
        jsonParam.put("requestTime", requestTime);
        jsonParam.put("token", token);
        List<String>strList = new ArrayList<>();
        strList.add(recNumber);

        jsonParam.put("strList",strList);
        jsonParam.put("message",content);
        StringEntity json = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
        json.setContentEncoding("UTF-8");
        json.setContentType("application/json");

        // System.out.println(jsonParam.toString());

        //添加参数
        try {
            post.setEntity(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");
             map = JSON.parseObject(result, Map.class);
            System.out.println("map:"+map);
            //map:
            System.out.println(map.get("result"));
            System.out.println("result:"+result);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 通话记录
     * @return
     */
    @Override
    public  Map<String ,Object>  tbCallRrcord() {
        Map<String, Object> map = new HashMap<>();
        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url+"/tisp/getVoiceRecord");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String spName = CommonConstant.spName;
        //获取通话记录表最大时间
        List<Map<String, Object>> list =  tbCallService.getMaxAddTime();
        Date addTime = null;
        String beginTime = null;
        String  endTime = null;
        if(!list.isEmpty()){
            addTime = (Date) list.get(0).get("add_time");
            //开始时间
            beginTime = sdf.format(addTime);
            Long endTimes =  addTime .getTime() + 60*1000*9 ;//9分钟
            Date date = new Date();
            date.setTime(endTimes);
            //结束时间
            endTime =  sdf.format(date);

        }else{
            //如果数据是空的时候，获取此刻前9分钟内的数据
           Long beginTimes =new Date().getTime()- 60*1000*9 ;//9分钟前
            Date date = new Date();
            date.setTime(beginTimes);
            //开始时间
            beginTime = sdf.format(date);
            //结束时间
            endTime =  sdf.format( new Date());
        }

        String requestTime = sdf.format(new Date());
        //开始时间
        //String beginTime  ="20180631000001";
        //结束时间
        //String endTime = "20181228000001";;
        String spPwd =CommonConstant.spPwd;
        String str = spName+requestTime+spPwd;
        MD5Util mD5Util = new MD5Util();
        String   token =   mD5Util.getMD5String(str);//MD5加密一次


        //把参数转换为json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("spName", spName);
        jsonParam.put("requestTime", requestTime);
        jsonParam.put("token", token);
        List<String>strList = new ArrayList<>();
        //strList.add("18955501400");
        strList = null;
        jsonParam.put("beginTime",beginTime);
        jsonParam.put("endTime",beginTime);
        jsonParam.put("strList",strList);//如果strList为空，取全部

        StringEntity json = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
        json.setContentEncoding("UTF-8");
        json.setContentType("application/json");

        System.out.println(jsonParam.toString());

        //添加参数
        try {
            post.setEntity(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");
            map = JSON.parseObject(result, Map.class);
            System.out.println("map:"+map);
            /**
             * 返回值
             * map:map:{result=1, list=[["18955501400","17406000014","2018-7-31.11.15. 9. 0","19","0"],["18955501400","17406000014","2018-7-31.11.15. 9. 0","19","0"],
             ["18955501400","17406000014","2018-7-31.11.15. 9. 0","19","0"],
             ["18955501400","17406000014","2018-7-31.11.15. 9. 0","19","0"]]}
             */
            System.out.println(map.get("result"));
            System.out.println("result:"+result);
            Integer  state = Integer.parseInt(map.get("result").toString());
            //  List<List>list = (List)map.get("list");

//            list.get(0).get(0);//List里面套List
//            System.out.println("list.get(0).get(0);"+ list.get(0).get(0));
//            System.out.println("list.get(0).get(1);"+ list.get(0).get(1));
//            System.out.println("list.get(0).get(2);"+ list.get(0).get(2));
//            System.out.println("list.get(0).get(3);"+ list.get(0).get(3));//20181109135233  时间需要格式化
            //还没写完

            // System.out.println("list.size():"+list.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  map;
    }

    /**
     * 流量记录
     * @return
     */
    @Override
    public void  tbSimFlowRrcord() {
        Map<String,Object>map = new HashMap<>();

        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url+"/tisp/simFlowRecord");

        String spName = CommonConstant.spName;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String requestTime = sdf.format(new Date());

        String spPwd =CommonConstant.spPwd;
        String str = spName+requestTime+spPwd;
        MD5Util mD5Util = new MD5Util();
        String   token =   mD5Util.getMD5String(str);//MD5加密一次



        //获取通话记录表最大时间
        List<Map<String, Object>> resultList =  tbSimFlowService.getMaxAddTime();
        //开始时间
        //String beginTime  ="20180631000001";
        //结束时间
       // String endTime = "20181228000001";;
        Date addTime = null;
        String beginTime = null;
        String  endTime = null;
        if(!resultList.isEmpty()){
            addTime = (Date) resultList.get(0).get("add_time");
            //开始时间
            beginTime = sdf.format(addTime);
            Long endTimes =  addTime .getTime() + 60*1000*9 ;//9分钟
            Date date = new Date();
            date.setTime(endTimes);
            //结束时间
            endTime =  sdf.format(date);

        }else{
            //如果数据是空的时候，获取此刻前9分钟内的数据
            Long beginTimes =new Date().getTime()- 60*1000*9 ;//9分钟前
            Date date = new Date();
            date.setTime(beginTimes);
            //开始时间
            beginTime = sdf.format(date);
            //结束时间
            endTime =  sdf.format( new Date());
        }

        //把参数转换为json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("spName", spName);
        jsonParam.put("requestTime", requestTime);
        jsonParam.put("token", token);
        List<String>strList = new ArrayList<>();
        strList = null;
        //strList.add("18955501400");

        jsonParam.put("beginTime",beginTime);
        jsonParam.put("endTime",beginTime);
        jsonParam.put("strList",strList);//如果strList为空，取全部
        StringEntity json = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
        json.setContentEncoding("UTF-8");
        json.setContentType("application/json");

        System.out.println(jsonParam.toString());

        //添加参数
        try {
            post.setEntity(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");
             map = JSON.parseObject(result, Map.class);
            System.out.println("map:"+map);
            /**
             * 返回值
             * map:map:{result=1, list=[["18955501400","112","19","0"]]}
             */
            System.out.println(map.get("result"));
            System.out.println("result:"+result);
            Integer  state = Integer.parseInt(map.get("result").toString());
            if(state==0||state==1){
                List<List>list = (List)map.get("list");

           String  phoneNumber = (String) list.get(0).get(0);
           String  surplusFlow =  (String) list.get(0).get(1);
           String  usedFlow= (String) list.get(0).get(2);
           String  totalFlow= (String) list.get(0).get(3);
           //通过电话号码获取卡的id
                List<Map<String, Object>>listMap =   tbSimFlowService.getIdByPhone(phoneNumber);
                if (!listMap.isEmpty()){
                    Long sim_info_id = (Long)listMap.get(0).get("sim_info_id");
                    EntityWrapper entityWrapper =    new EntityWrapper<>();
                    entityWrapper.eq("sim_info_id",sim_info_id);
                    TbSimFlow tbSimFlow = tbSimFlowService.selectOne(entityWrapper);

                    tbSimFlow.setUsedFlow(usedFlow);
                    tbSimFlow.setSurplusFlow(surplusFlow);
                    tbSimFlow.setTotalFlow(totalFlow);

                    tbSimFlowService.insert(tbSimFlow);
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 资费记录
     * @return
     */
    @Override
    public List<Map<String, Object>> tbSimChargesRrcord() {
        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url+"/tisp/getCharge");
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        String spName = CommonConstant.spName;//还没有这个参数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String requestTime = sdf.format(new Date());
        String spPwd = CommonConstant.spPwd;//还没有这个参数
        String token = spName+requestTime+spPwd;
        //token是否需要MD5加密
        MD5Util mD5Util = new MD5Util();
        token =   mD5Util.getMD5String(token);//MD5加密一次
        params.add(new BasicNameValuePair("token",token));
        //添加参数
        try {
            post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();
            String result =EntityUtils.toString(entity, "utf-8");
            System.out.println(result);


        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
    public  Map<String,Object> getMAXaddTime(){
        Map<String,Object>map = new HashMap<>();

        PrepareCall prepareCall = new PrepareCall();
        Connection conn = prepareCall.getConnection();  //创建数据库连接
        String beginTime = null;
        Date endTimes = null;
        try {
            //调用存储过程
            CallableStatement cs = conn.prepareCall("{call sp_getTrajectoryMAXaddTime(?)}");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String yearMouth =null;
            Calendar cale =  Calendar.getInstance();
            Integer year = (cale.get(Calendar.YEAR));//2019
            Integer month = cale.get(Calendar.MONTH) + 1;//5
            if(month<10){
                yearMouth =  year.toString()+"0"+month.toString();
            }else{
                yearMouth =  year.toString()+month.toString();
            }
            cs.setString(1,yearMouth);
            ResultSet rs = cs.executeQuery(); //执行操作，并获取结果集


            while(rs.next())
            {

                if(rs.getTimestamp("add_time")!=null){
                    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    beginTime = sdf.format(rs.getTimestamp("add_time"));//20181228000001
                    map.put("beginTime",beginTime);
                    endTimes =rs.getTimestamp("add_time");
                    //通过时间戳转换
                    // 60*1000*5 这是5分钟
                    Long t =    endTimes .getTime() + 60*1000*9 ;//9分钟
                    Date afterDate = new Date();
                    afterDate.setTime(t);
                    String  endTime = sdf.format(afterDate );
                    map.put("endTime",endTime);
                    //System.out.println("afterDate:"+sdf.format(afterDate ));//最大时间加9分钟之后的时间

                }else{
                    //如果是第一次初始的时候没有时间
                    Long beginTimes =new Date().getTime()- 60*1000*9 ;//9分钟前
                    Date date = new Date();
                    date.setTime(beginTimes);
                    //开始时间
                    beginTime = sdf.format(date);
                    //结束时间
                    String  endTime  =  sdf.format( new Date());
                    map.put("beginTime",beginTime);
                    map.put("endTime",endTime);
                }
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    public  Map<String,Object> applyRescueMessage() {
        Map<String, Object> map = new HashMap<>();
        HttpClient client = HttpClientBuilder.create().build();
        //ip和端口不知道
        HttpPost post = new HttpPost(CommonConstant.url+"/tisp/applyRescueMessage");

        String spName = CommonConstant.spName;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String requestTime = sdf.format(new Date());

        String spPwd =CommonConstant.spPwd;
        String str = spName+requestTime+spPwd;
        MD5Util mD5Util = new MD5Util();
        String   token =   mD5Util.getMD5String(str);//MD5加密一次

        //把参数转换为json格式
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("spName", spName);
        jsonParam.put("requestTime", requestTime);
        jsonParam.put("token", token);
        List<String>strList = new ArrayList<>();
        //strList = null;
        strList.add("18955501400");
        strList.add("18955501401");

        jsonParam.put("strList",strList);//如果strList为空，取全部
        StringEntity json = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
        json.setContentEncoding("UTF-8");
        json.setContentType("application/json");

        System.out.println(jsonParam.toString());

        //添加参数
        try {
            post.setEntity(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response =  client.execute(post);
            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");
            map = JSON.parseObject(result, Map.class);
            // 返回值 result:{"result":"0"}
            System.out.println("map:"+map);
            System.out.println(map.get("result"));
            System.out.println("result:"+result);


        } catch (IOException e) {
            e.printStackTrace();
        }

        return map;
    }
}
